#!/usr/bin/env python3
"""
插入列对话框 - 允许用户在指定位置插入列
"""

from PySide6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QWidget,
                               QPushButton, QLabel, QLineEdit, QSpinBox,
                               QFormLayout, QDialogButtonBox, QMessageBox)
from PySide6.QtCore import Qt
from typing import Optional, Tuple


class InsertColumnDialog(QDialog):
    """插入列对话框"""
    
    def __init__(self, total_columns: int, parent=None):
        super().__init__(parent)
        self.total_columns = total_columns
        
        self.setWindowTitle("插入列")
        self.setModal(True)
        self.setFixedSize(400, 200)
        
        self._setup_ui()
        
    def _setup_ui(self):
        """设置UI"""
        layout = QVBoxLayout(self)
        
        # 表单布局
        form_layout = QFormLayout()
        
        # 列名输入
        self.column_name_edit = QLineEdit()
        self.column_name_edit.setPlaceholderText("请输入列名")
        form_layout.addRow("列名:", self.column_name_edit)
        
        # 插入位置选择
        self.position_spinbox = QSpinBox()
        self.position_spinbox.setMinimum(0)
        self.position_spinbox.setMaximum(self.total_columns)
        self.position_spinbox.setValue(0)
        self.position_spinbox.setSuffix(f" (0-{self.total_columns})")
        form_layout.addRow("插入位置:", self.position_spinbox)
        
        # 默认值输入
        self.default_value_edit = QLineEdit()
        self.default_value_edit.setPlaceholderText("可选，留空表示空值")
        form_layout.addRow("默认值:", self.default_value_edit)
        
        layout.addLayout(form_layout)
        
        # 说明文本
        info_label = QLabel(f"注意：插入位置为0表示在最前面，{self.total_columns}表示在最后面")
        info_label.setStyleSheet("color: gray; font-size: 10px;")
        layout.addWidget(info_label)
        
        # 按钮
        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)
        
        # 设置焦点
        self.column_name_edit.setFocus()
        
    def get_column_info(self) -> Optional[Tuple[int, str, str]]:
        """获取列信息：位置、列名、默认值"""
        if self.result() == QDialog.Accepted:
            column_name = self.column_name_edit.text().strip()
            if not column_name:
                QMessageBox.warning(self, "警告", "请输入列名")
                return None
            
            position = self.position_spinbox.value()
            default_value = self.default_value_edit.text().strip()
            
            return position, column_name, default_value
        
        return None 